package com.ideabove.heart;

import java.io.IOException;
import java.util.Timer;

import com.ideabove.heart.manager.AppManager;
import com.ideabove.heart.manager.MoeManager;
import com.ideabove.heart.manager.SoundManager;
import com.ideabove.heart.util.LogUtil;
import com.ideabove.heart.util.MyUtil;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;

public class SendingActivity extends MyActivity {
	Activity mActivity;
	int mcount = 0;
	MediaPlayer mPlayer = new MediaPlayer();
	Timer mTimer;
	SoundManager soundMgr;
	int SOUND_IDX = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calling);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
				| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
				| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 

		mActivity = this;

		// 효과음
		soundMgr = new SoundManager(this);
		soundMgr.addSound(SOUND_IDX, R.raw.button_press);
		AppManager.pause(this);
		
		// 텍스트
		final TextView textView = (TextView)findViewById(R.id.main_calling_txt);
		textView.setText("영상전화 발신 중");
		MyUtil.blinkText(textView, 700);

		AssetFileDescriptor afd;
		try {
			afd = getAssets().openFd("voice/conecting.mp3");
			mPlayer = new MediaPlayer();
			mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
			afd.close();			
			mPlayer.prepare();
			mPlayer.start();

			mPlayer.setOnErrorListener(new OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub
					((Activity)MoeManager.mCurContext).finish();
					return false;
				}
			});

			mPlayer.setOnCompletionListener(new OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					mcount++;
					if(mcount < 1){						
						mPlayer.start();						
					}else{						
						// 모에 페이지로 이동한다.						
						Intent intent = new Intent(SendingActivity.this, MoeActivity.class);
						SendingActivity.this.startActivity(intent);
						SendingActivity.this.finish();
						
					}
				}
			});

		} catch (IOException e) {
			// TODO Auto-generated catch block
			if(MoeManager.mCurContext != null){
				((Activity)MoeManager.mCurContext).finish();
			}
			e.printStackTrace();		
		}

		// 전화끊기버튼
		ImageView main_call_btn = (ImageView)findViewById(R.id.hang_up_btn);
		main_call_btn.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {				
				// TODO Auto-generated method stub
				MyUtil.touchMe(v, event, new MyUtil.ImageTouch() {

					@Override
					public void touch(View v, MotionEvent event) {
						// TODO Auto-generated method stub	
						
						if(mPlayer != null){
							mPlayer.stop();
						}
						
						soundMgr.playSound(SOUND_IDX);
						finish();						
					}
				});
				return true;
			}
		});
	}
	
	

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AppManager.pause(SendingActivity.this);
	}



	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AppManager.pause(SendingActivity.this);		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if(mPlayer != null){
			mPlayer.stop();
		}
		if(soundMgr != null){
			soundMgr.clear();
		}
		
		super.onDestroy();
	}

}
